package com.mmlc.meimei.lichengkongjian.classify;

import android.app.AlertDialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.androidnetframe.utils.LogUtils;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.Order;
import com.mmlc.meimei.lichengkongjian.mine.ActivityOrderDetail;
import com.mmlc.network.filter.GoPayRequestFilter;
import com.mmlc.network.responsebean.GoPayResponseBean;
import com.mmlc.network.responsebean.SubmitResponseBean;
import com.mmlc.utils.PayUtils;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;


public class SubmitSuccessActivity extends InterActivity {

    @Override
    protected int initContentView() {
        return R.layout.activity_submit_success;
    }

    SubmitResponseBean.DataBean submitSuccessOrder;
    String order_id, order_price, companyName = "";
    int modeInvoice = 0;
    String pay_code,pay_name;
    PayUtils payUtils;
    Order order;


    @Override
    protected void initExtra() {
        super.initExtra();
        submitSuccessOrder = (SubmitResponseBean.DataBean) getIntent().getSerializableExtra("order");
        if (submitSuccessOrder != null) {
            order_id = submitSuccessOrder.getOrder_id();
            order_price = submitSuccessOrder.getOrder_price();
            pay_code = submitSuccessOrder.getPay_code();
            pay_name = submitSuccessOrder.getPay_name();
            order = new Order(submitSuccessOrder.getOrder_id(), submitSuccessOrder.getOrder_price(),submitSuccessOrder.getPay_name(), submitSuccessOrder.getPay_code(), submitSuccessOrder.getTitle(), submitSuccessOrder.getBody(), submitSuccessOrder.getTimestamp(), submitSuccessOrder.getNotify_url(), submitSuccessOrder.getPay_url(),submitSuccessOrder.getExt1(),submitSuccessOrder.getGift_score());
        }
//        address_id = getIntent().getStringExtra("address_id");
        modeInvoice = getIntent().getIntExtra("modeInvoice", 0);
        if (modeInvoice == 2) {
            companyName = getIntent().getStringExtra("companyName");
        }

        payUtils = new PayUtils(SubmitSuccessActivity.this);
    }
    RelativeLayout rl_mode_pay;
    TextView tv_pay_content,tv_sub, tv_number, tv_name, tv_phone, tv_address, tv_price , tv_hintContent;

    @Override
    protected void initView() {
        tv_number = ViewUtils.getView(activity, R.id.tv_number);
        tv_name = ViewUtils.getView(activity, R.id.tv_name);
        tv_phone = ViewUtils.getView(activity, R.id.tv_phone);
        tv_address = ViewUtils.getView(activity, R.id.tv_address);
        tv_price = ViewUtils.getView(activity, R.id.tv_price);
        tv_hintContent = ViewUtils.getView(activity, R.id.tv_hintContent);

        tv_sub = ViewUtils.getView(activity, R.id.tv_sub);
        tv_sub.setOnClickListener(this);
        ViewUtils.getView(activity, R.id.iv_back).setOnClickListener(this);

        tv_pay_content = ViewUtils.getView(activity, R.id.tv_pay_content);
        rl_mode_pay = ViewUtils.getView(activity, R.id.rl_mode_pay);
        String tip = getIntent().getStringExtra("tip");
        if(!TextUtils.isEmpty(tip)){
            tv_hintContent.setText(tip);
        }
        tv_number.setText(order_id);
        tv_price.setText("¥ " + order_price);
        tv_pay_content.setText(pay_name);
        rl_mode_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(activity, ModePaySelectActivity.class).putExtra("type","payagain" ), 808);
            }
        });
    }

//    AddressDetailResponseBean.DataBean data;

//    @Override
//    protected void initInfo() {
//        super.initInfo();
//
//        AddressDetailRequestFilter filter = new AddressDetailRequestFilter(activity);
//        filter.requestBean.id = address_id;
//        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<AddressDetailResponseBean>() {
//            @Override
//            public void onSuccess(AddressDetailResponseBean result) {
//                data = result.getData();
//                tv_name.setText(data.getContactor());
//                tv_phone.setText(data.getPhone());
//                tv_address.setText(data.getProvince_name() + "/" + data.getCity_name() + "/" + data.getArea_name() + "/" + data.getAddress());
//            }
//
//            @Override
//            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
//                data = new AddressDetailResponseBean.DataBean();
//            }
//        });
//    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (exitDialog != null && exitDialog.isShowing())
                exitDialog.hide();
            else initExitDialog();

            return false;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                initExitDialog();
                break;
            case R.id.tv_sub:
                payUtils.toPay(order, new PayUtils.OnPayResultListener() {
                    @Override
                    public void payResult(int payType, int payResult) {
                        LogUtils.e("payResult",payResult+"");
                        if (payResult == 0) {
                            Intent intent = new Intent(activity, PayResultActivity.class);
                            intent.putExtra("order", order);
                            startActivity(intent);
                            finish();
                        } else
                            initResultDialog(payResult);
                    }
                });
                break;
        }
    }

    AlertDialog exitDialog;

    private void initExitDialog() {
        exitDialog = new AlertDialog.Builder(activity).create();
        exitDialog.setView(getLayoutInflater().inflate(R.layout.dialog_exit, null));
        exitDialog.show();
        exitDialog.getWindow().setGravity(Gravity.CENTER);
        exitDialog.getWindow().setLayout(android.view.WindowManager.LayoutParams.WRAP_CONTENT, android.view.WindowManager.LayoutParams.WRAP_CONTENT);
        exitDialog.getWindow().setContentView(getLayoutInflater().inflate(R.layout.dialog_exit, null));

        ViewUtils.getView(exitDialog, R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                /** 离开 */
                exitDialog.hide();
                finish();
            }
        });
        ViewUtils.getView(exitDialog, R.id.tv_verify).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /** 留下 */
                exitDialog.hide();
            }
        });
    }

    AlertDialog resultDialog;

    private void initResultDialog(final int payResult) {
        resultDialog = new AlertDialog.Builder(activity).create();
        resultDialog.setCancelable(false);
        resultDialog.setCanceledOnTouchOutside(false);
        resultDialog.setView(getLayoutInflater().inflate(R.layout.dialog_exit, null));
        resultDialog.show();
        resultDialog.getWindow().setGravity(Gravity.CENTER);
        resultDialog.getWindow().setLayout(android.view.WindowManager.LayoutParams.WRAP_CONTENT, android.view.WindowManager.LayoutParams.WRAP_CONTENT);
        resultDialog.getWindow().setContentView(getLayoutInflater().inflate(R.layout.dialog_exit, null));

        ((TextView) ViewUtils.getView(resultDialog, R.id.tv_title)).setText("支付结果");
        TextView tv_message = ViewUtils.getView(resultDialog, R.id.tv_message);
        if (payResult == 0) {
            tv_message.setText("支付成功");

            ((TextView) ViewUtils.getView(resultDialog, R.id.tv_cancel)).setText("继续购物");
            ((TextView) ViewUtils.getView(resultDialog, R.id.tv_verify)).setText("查看详情");
        } else if (payResult == 1) {
            tv_message.setText("支付失败");

            ((TextView) ViewUtils.getView(resultDialog, R.id.tv_cancel)).setText("取消");
            ((TextView) ViewUtils.getView(resultDialog, R.id.tv_verify)).setText("再次支付");
        }
        ViewUtils.getView(resultDialog, R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                resultDialog.hide();
                finish();
            }
        });

        ViewUtils.getView(resultDialog, R.id.tv_verify).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (payResult == 0) {
                    startActivity(new Intent(activity, ActivityOrderDetail.class));
                } else if (payResult == 1) {
                    resultDialog.hide();
                    payUtils.toPay(order);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        payUtils.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(data!=null && requestCode == 808 && resultCode == 200 ){
            String code = data.getStringExtra("code");
            String name = data.getStringExtra("name");
            tv_pay_content.setText(name);
            goPayDetail(order_id,code);
            tv_sub.setBackgroundColor(getResources().getColor(R.color.gray_pressed));
        }else {
            payUtils.onActivityResult(requestCode, resultCode, data);
        }

    }

    public void goPayDetail(final String orderId, String code) {
        GoPayRequestFilter filter = new GoPayRequestFilter(activity);
        filter.requestBean.method = "goPay";
        filter.requestBean.order_id = orderId;
        filter.requestBean.pay_code = code;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GoPayResponseBean>() {

            @Override
            public void onSuccess(GoPayResponseBean result) {
                order.setOrder_id(result.getData().getOrder_id());
                order.setOrder_price(result.getData().getOrder_price());
                order.setPay_name(result.getData().getPay_name());
                order.setPay_code(result.getData().getPay_code());
                order.setTitle(result.getData().getTitle());
                order.setBody(result.getData().getBody());
                order.setTimestamp(result.getData().getTimestamp()+"");
                order.setNotify_url(result.getData().getNotify_url());
                order.setPay_url(result.getData().getPay_url());
                order.setExt1(result.getData().getExt1());
                tv_sub.setBackgroundColor(getResources().getColor(R.color.black_deep));
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.e("SubmitSuccessActivity","onDestroy");
    }
}
